package com.igeek.shop.web.login;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import com.igeek.common.utils.CommonUtils;
import com.igeek.common.utils.MailUtils;
import com.igeek.shop.entity.User;
import com.igeek.shop.service.UserService;

/**
 * 
* @ClassName: RegistServlet  
* @Description: 用户注册的web实现层
* @date 2017年12月12日 下午4:21:12    
* Company www.igeekhome.com
*
 */
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//0.解决中文乱码问题
		request.setCharacterEncoding("utf-8");
		
		User user = new User();
		//1.获取表单中的数据
		try {
			
			//手动将String转成Date类型
			ConvertUtils.register(new Converter() {
			
				@Override
				public Object convert(Class clazz, Object value) {
					SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
					Date desc = null;
					try {
						desc = sf.parse((String)value);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return desc;
				}
			}, Date.class);
			
			////2.组装成User实体对象
			BeanUtils.populate(user, request.getParameterMap());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//uid,state(默认就是0),code（激活码）
		//UUID生成不重复的字符串
		user.setUid(CommonUtils.getUUID());
		String activeCode =CommonUtils.getUUID(); 
		user.setCode(activeCode);
		//验证码验证
		String checkbox = request.getParameter("checkCode");
		String sessionCode = (String)request.getSession().getAttribute("CHECK_CODE_KEY");
		boolean flag = false;
		System.out.println(checkbox);
		System.out.println(sessionCode);
		if(sessionCode!=null&&checkbox.equalsIgnoreCase(sessionCode)){
			request.getSession().removeAttribute("CHECK_CODE_KEY");
			flag= true;
		}else{
			flag = false;
			request.setAttribute("massage", "验证码错误！");
			response.sendRedirect(request.getContextPath()+"/registerFail.jsp");
			return;
		}
		
		
		//3.调用service的注册的方式
		UserService service = new UserService();
		boolean isSuccess= service.regist(user);
		System.out.println(isSuccess);
		
		System.out.println(flag);
		request.setAttribute("flag",flag);
		//如果注册成功则进入循环
		if((isSuccess==true)&&(flag==true)){
					String emailMsg = "恭喜您，注册成功，请点击下方链接激活邮箱！<br>"
					+ "<a href='http://localhost:8080/IGEEKShop/active?activeCode="+activeCode+"'>http://localhost:8080/IGEEKShop/active?activeCode="+activeCode+"</a>";
			try {
				MailUtils.sendMail(user.getEmail(), emailMsg);
			} catch (AddressException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			response.sendRedirect(request.getContextPath()+"/registerSuccess.jsp");
		}else{
			response.sendRedirect(request.getContextPath()+"/registerFail.jsp");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	

}
